package com.demonzym.tv.api;

import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import android.util.Log;

import com.demonzym.tv.app.MyApplication;
import com.demonzym.tv.util.ConstValue;
import com.tudou.api.bean.TvProgress;

public class Api {
	private static Api mInstance;
	public static Api getInstance(){
		if(mInstance == null)
			mInstance = new Api();
		return mInstance;
	}
	
	private FinalHttp mFinalHttp;
	public Api(){
		mFinalHttp = MyApplication.getInstance().finalHttp;
	}
	
	public void doGetPlayListInfos(AjaxCallBack acb){
		AjaxParams ap = new AjaxParams();
		ap.put("method", "playlist.info.get");
		ap.put("appKey", ConstValue.TUDOU_APPKEY);
		ap.put("format", "json");
		ap.put("playlistCodes", ConstValue.TUDOU_PLAYLISTID);
		ap.put("ceiling", "10");
		mFinalHttp.get(ConstValue.TUDOU_API, ap, acb);
	}
	public void doGetItemInfos(String itemCodes, AjaxCallBack acb){
		AjaxParams ap = new AjaxParams();
		ap.put("method", "item.info.get");
		ap.put("appKey", ConstValue.TUDOU_APPKEY);
		ap.put("format", "json");
		ap.put("itemCodes", itemCodes);
		mFinalHttp.get(ConstValue.TUDOU_API, ap, acb);
	}
	public void doGetVideoUrl(String itemCode, AjaxCallBack acb){
		mFinalHttp.get(ConstValue.TUDOU_CONVERT_VIDEO_URL + itemCode, acb);
	}
	
	
	
	public void doGetAlbum(AjaxCallBack acb){
		AjaxParams ap = new AjaxParams();
		ap.put("pid", "8084620b90d000d0");
		ap.put("t", "1368686081");
		ap.put("e", "md5");
		ap.put("s", "bb168eedd05bfae6bf5917abf4d8723e");
		ap.put("guid", "250fd60d88b9260ef8f08fcdbef3400b");
		ap.put("albumid", ConstValue.ALBUM_ID);
		ap.put("pg", "1");
		ap.put("platform", "1");
		mFinalHttp.get(ConstValue.TUDOU_ALBUM, ap, acb);
	}
	public void doGetItem(String id, AjaxCallBack acb){
		AjaxParams ap = new AjaxParams();
		ap.put("pid", "8084620b90d000d0");
		ap.put("t", "1368686084");
		ap.put("e", "md5");
		ap.put("s", "e34ce6b72fc31e15e945dcf89fa005a5");
		ap.put("guid", "250fd60d88b9260ef8f08fcdbef3400b");
		ap.put("id", id);
		ap.put("youku_format", "4");
		ap.put("audiolang", "1");
		ap.put("language", "guoyu");
		ap.put("point", "1");
		ap.put("local_time", "");
		ap.put("local_vid", "");
		ap.put("local_point", "");
		mFinalHttp.get(ConstValue.TUDOU_PLAY, ap, acb);
	}
	
	public TvProgress doGetLastHistory(){
		TvProgress pro = null;
		List<TvProgress> list = 
				MyApplication.getInstance().finalDb.findAll(TvProgress.class, "id");
		if(list.size() == 0)
			return null;
		pro = list.get(0);
		return pro;
	}
	
	public void doSaveTvProgress(String title, String id, int progress){
		TvProgress tvP = new TvProgress(title, id, progress);
		MyApplication.getInstance().finalDb.deleteByWhere(
				TvProgress.class, "\"itemId\"=\"" + id + "\"");
		MyApplication.getInstance().finalDb.save(tvP);
	}
}
